home *** CD-ROM | disk | FTP | other *** search
- Path: telepost.no!usenet
- From: Carsten Arnholm <ca@sesam.dnv.no>
- Newsgroups: comp.lang.fortran,comp.lang.c++
- Subject: Re: How to link Fortran with c++ ??
- Date: 2 Feb 1996 11:36:15 GMT
- Organization: DNV
- Message-ID: <4essvf$3ar@nms.telepost.no>
- References: <310DD74F.59E2@imacsg1.epfl.ch> <4env5h$3ai@nms.telepost.no> <3110887E.41C6@imacsg1.epfl.ch>
- NNTP-Posting-Host: hugin.sesam.dnv.no
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1 (Windows; I; 32bit)
-
- Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
- >Carsten Arnholm wrote:
- >>
- >> Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
- >
- >> >I have to link a piece of fortran code with c++ code but since if never
- >> >done that before I'd like to know a) can it be done and b) is it
- >> >difficult.
- >> >
- >> >Everything concerning this subject is welcome.
- >>
- >> Hi there,
- >>
- >> Mixed language programming (C++/FORTRAN 77) is not as hard as
- >> it may seem.
- >>
- >> The only non-trivial problem you must solve is fortran CHARACTERs.
- >> You need to write a minimal CHARACTER class in C++.
- >>
- >> If you require single-source portability, it becomes slightly
- >> more complicated, but not impossible.
- >>
- >> Below follows a summary of some stuff that I have done, plus
- >> some additional thoughts.
- >
- >As understand it, this is the way to go if you want to call fortran routines
- >in a c++ code. But what if you want to do go the other way around ?
- >
- >So how do I call an data member from a c++ class in fortran ?
- >say I want to call a member function called GetData() in fortran so
- >how do I transplate Class->GetData() in fortran ?
- >
- >Thanxs,
- >Niels
-
-
- Well, I have never done that, but generally I guess you would have to
-
- a) Write a C-API (i.e. a set of C++ functions with C linkage) for the
- things you want to call from FORTRAN. Do this by declaring the API
- functions as extern "C" (avoids name mangling).
-
- b) Provide some kind of pointer in the API to identify objects. These
- pointers are not used by FORTRAN, they're just object identifiers.
-
-
- Here's my suggestion (I don't know if it works).
-
-
- C++:
-
- class myClass {
- public:
- myMemFunc();
- }
-
-
- C-API:
-
- extern "C" int myClass_create()
- {
- myClass* myObject = new myClass;
- return (int) (*myObject);
- }
-
- extern "C" void myClass_myMemFunc(int& object)
- {
- myClass* myObject = (myClass*) &object;
- myObject->myMemFunc();
- }
-
-
- FORTRAN:
-
- PROGRAM TEST
- INTEGER MYOBJ
- MYOBJ = myClass_create();
- myClass_myMemFunc(MYOBJ)
- END
-
-
- Anyone done this, or similar ?
-
- Carsten
- ca@sesam.dnv.no
-
-
-